#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "hiredisutils.h"

redisContext *globalRedisCtx;

redisContext* redis_connect(char *ip, int port)
{
	redisContext *c = NULL;
	struct timeval timeout = { 3, 000000 }; // 3.0 seconds
	char host[51];

	memset(host, 0x00, sizeof(host));
	if (ip) {
		strcpy(host, ip);
	}
	else {
		strcpy(host, "127.0.0.1");
	}

	c = redisConnectWithTimeout(host, port, timeout);
	if (c == NULL || c->err) {
		if (c) {
			redisFree(c);
			c = NULL;
			
			return NULL;
		} 
		else {
			return NULL;
		}
	}

	globalRedisCtx = c;

	return c;
}

redisReply* redis_command(char *commandBuf)
{
        redisReply *reply = NULL;

        reply = redisCommand(globalRedisCtx, commandBuf);
	
	return reply;
}
